package com.gientech.controller;

import cn.hutool.core.collection.CollUtil;
import com.gientech.entity.CommonResult;
import com.gientech.entity.RoleInfo;
import com.gientech.entity.UserInfo;
import com.gientech.service.MenuService;
import com.gientech.service.RoleService;
import com.gientech.service.UserService;
import com.gientech.utils.UserInfoContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Controller
@RequestMapping("admin")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录以后返回token
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/login")
    @ResponseBody
    public CommonResult login(@Validated @RequestBody UserInfo userInfo) {
        logger.info("登录系统>>>>>>>>>>>>>>{}", userInfo);
        String token = userService.login(userInfo.getUsername(), userInfo.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        logger.info("获取的token>>>>>>>>>>>>>{}", token);
        return CommonResult.success(tokenMap);
    }

    /**
     * 获取当前登录用户信息
     * @return
     */
    @RequestMapping(value = "/info")
    @ResponseBody
    public CommonResult getUserInfo() {
        logger.info("获取当前登录用户信息>>>>>>>>>>>>>>");
        UserInfo userInfo = UserInfoContextHolder.getUserInfo();
        if(userInfo==null){
            return CommonResult.unauthorized(null);
        }
        String username = userInfo.getUsername();
        UserInfo user = userService.getUserInfoByUsername(username);
        Map<String, Object> data = new HashMap<>();
        data.put("username", user.getUsername());
        data.put("icon", user.getIcon());
        data.put("menus", menuService.getMenuListByUserId(user.getId()));

        List<RoleInfo> roleList = roleService.getRoleListByUserId(user.getId());
        if(CollUtil.isNotEmpty(roleList)){
            List<String> roles = roleList.stream().map(RoleInfo::getName).collect(Collectors.toList());
            data.put("roles",roles);
        }
        // 将查询到的用户信息放入redis中
        redisTemplate.opsForValue().set(user.getUsername(), user);
        return CommonResult.success(data);
    }
}
